import type { User, Customer } from "@/service/api/types/user";
import type { Response } from "@/service/api/types/request";
import type { MockAdapterInstance } from "../index";
import _ from "lodash";

export default function userMock(mock: MockAdapterInstance) {
    mock.onPost("/user/token").reply((config) => {
        const response: Response<{
            userInfo: User | Customer;
            token: string;
        } | null> = {} as Response<null>;

        const { token } = JSON.parse(config.data);

        if (token === "123456") {
            response.code = 1;
            response.message = "验证成功";
            response.data = {
                userInfo: {
                    id: "1",
                    account: "114514191",
                    username: "admin",
                    role: "admin",
                    frozen: false,
                    createdDate: Date.now(),
                },
                token: "123456",
            };
            return [200, response];
        } else if (token === "114514") {
            response.code = 1;
            response.message = "验证成功";
            response.data = {
                userInfo: {
                    id: "2",
                    account: "114514192",
                    username: "季伯常",
                    role: "customer",
                    frozen: false,
                    phone: "11451419198",
                    companyName: "季伯市季伯区季伯有限公司",
                    createdDate: Date.now(),
                },
                token: "114514",
            };
            return [200, response];
        }
        response.code = 0;
        response.message = "验证失败";
        response.data = null;
        return [200, response];
    });

    mock.onPost("/user/login").reply((config) => {
        const { account, password } = JSON.parse(config.data);

        const response: Response<{
            userInfo: User | Customer;
            token: string;
        } | null> = {} as Response<null>;

        if (account === "114514191" && password === "12345678") {
            response.code = 1;
            response.message = "登录成功";
            response.data = {
                userInfo: {
                    id: "1",
                    account: "114514191",
                    username: "admin",
                    role: "admin",
                    frozen: false,
                    createdDate: Date.now(),
                },
                token: "123456",
            };
        } else if (account === "114514192" && password === "12345678") {
            response.code = 1;
            response.message = "登录成功";
            response.data = {
                userInfo: {
                    id: "2",
                    account: "114514192",
                    username: "季伯常",
                    role: "customer",
                    frozen: false,
                    phone: "11451419198",
                    companyName: "季伯市季伯区季伯有限公司",
                    createdDate: Date.now(),
                },
                token: "114514",
            };
        } else {
            response.code = 0;
            response.message = "账号或密码错误";
            response.data = null;
        }
        return [200, response];
    });

    mock.onPost("/user/avatar").reply((config) => {
        const { avatar } = JSON.parse(config.data);
        const response: Response<string> = {} as Response<string>;
        response.code = 1;
        response.message = "上传成功";
        response.data = avatar;
        return [200, response];
    });
}
